const Router = require('koa-router')
const { koaBody } = require('koa-body')

// 导入中间件
const {
    userIsExist,
    userRegisterValidator,
    userLoginValidator,
    verifyRegister,
    userModifyValidator,
    verifyLogin,
    cryptPassword,
    avatarKoaBody,
    uploadAvatarFormat,
    verifyAdminRole,
    userTelValidator,
    isOpenId
} = require('../middleware/user.middleware')
const { auth } = require('../middleware/auth.middleware')

// 导入controller层
const {
    login,
    logout,
    getUserInfo,
    addUser,
    getUserList,
    modifyPwd,
    modifyInfoByUserAccount,
    deleteUser,
    uploadAvatar,
    modifyAccountActiveStatus,
    attentionACT,
    getAttentionActList,
    isAttention,
    cancelAttention,
    wxlogin,
    batchRegister,
    userIsExistController
} = require('../controller/user.controller')

// 创建地址为‘/user/xxx’的api地址
const router = new Router({ prefix: '/user' })

// 批量注册
router.post('/batchRegister', auth, koaBody({ multipart: true }), batchRegister)
// 新增(注册)用户
router.post('/add', koaBody({ multipart: true }), userRegisterValidator, verifyRegister, cryptPassword, addUser)
// 登录用户接口
router.post('/login', koaBody({ multipart: true }), isOpenId, userLoginValidator, verifyLogin, login)
// 微信登录
router.post('/wxlogin', koaBody({ multipart: true }), userLoginValidator, verifyLogin, wxlogin)
// 用户登出(注销)
router.post('/logout', koaBody({ multipart: true }), auth, logout)
// 获取用户信息
router.get('/info', koaBody({ multipart: true }), auth, getUserInfo)
// 获取用户列表
router.get('/getUserList', koaBody({ multipart: true }), auth, getUserList)
// 修改用户基本信息
router.post('/modifyUserInfo', koaBody({ multipart: true }), auth, userIsExist, userModifyValidator, userTelValidator, modifyInfoByUserAccount)
// 修改密码
router.post('/modifyPwd', koaBody({ multipart: true }), auth, userIsExist, cryptPassword, modifyPwd)
// 根据用户账号删除该用户
router.post('/deleteUser', koaBody({ multipart: true }), auth, verifyAdminRole, userIsExist, deleteUser)
// 头像修改
router.post('/uploadAvatar', auth, avatarKoaBody, uploadAvatarFormat, uploadAvatar)
// 更改用户账号激活状态
router.post('/isActive', koaBody({ multipart: true }), auth, verifyAdminRole, userIsExist, modifyAccountActiveStatus)
// 判断用户是否存在
router.post('/userIsExist', auth, koaBody({ multipart: true }), userIsExist, userIsExistController)

// 关注活动
router.post('/attentionACT', koaBody({ multipart: true }), auth, attentionACT)
// 获取我的关注列表
router.get('/getAttentionActList', auth, getAttentionActList)
// 是否已关注活动
router.get('/isAttention', auth, isAttention)
// 取消关注
router.post('/cancelAttention', auth, koaBody({ multipart: true }), cancelAttention)


module.exports = router